package com.wjjun.base.ui.fragment

import android.view.LayoutInflater
import android.view.View
import androidx.databinding.ViewDataBinding
import com.wjjun.base.ui.helper.RecycleViewHelper
import com.wjjun.base.ui.helper.RefreshRecycleViewHelper
import com.wjjun.base.ui.listener.IItemViewListener
import com.wjjun.base.ui.viewmodel.BaseItemViewModel
import com.wjjun.base.ui.viewmodel.BaseWindowViewModel
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * 列表Fragment
 * author : wangjunjun
 * date   : 2022/6/9
 * desc   :
 */
abstract class BaseListFragment<VM : BaseWindowViewModel, VD : ViewDataBinding> :
        BaseLazyFragment<VM, VD>(), RefreshRecycleViewHelper.DataProvide, RecycleViewHelper.ListItemBinding,
    IItemViewListener {

    var mRecycleViewHelper = RefreshRecycleViewHelper()

    override fun initViews(inflater: LayoutInflater): View {
        val view = super.initViews(inflater)
        mRecycleViewHelper.setLifecycleOwner(viewLifecycleOwner)
        mRecycleViewHelper.listItemBinding = this
        mRecycleViewHelper.dataProvide = this
        mRecycleViewHelper.onItemClickListener = this
        return view
    }

    /**
     * 绑定item时的回调
     * 如果要绑定额外的变量或者事件，
     * 请使用itemBinding.bindExtra()
     */
    override fun onListItemBind(
            itemBinding: ItemBinding<*>,
            position: Int,
            itemViewModel: BaseItemViewModel<*>
    ) {

    }

    override fun initLazyContent(viewDataBinding: VD) {
        mRecycleViewHelper.bindViewDataBinding(viewDataBinding)
    }

    override fun onRequestPermission(viewModel: BaseWindowViewModel) {
        initData()
    }

    override fun onRefresh(viewModel: BaseWindowViewModel) {
        refreshData()
    }

    fun getMainRecycleViewHelper() : RecycleViewHelper{
        return  mRecycleViewHelper
    }

    fun refreshData(useCache: Boolean = false) {
        mRecycleViewHelper.refreshData(useCache)
    }

    fun refreshDataShowAnim() {
        mRecycleViewHelper.refreshDataShowAnim()
    }

    /**
     * 通知更新数据，如果界面不在前台，则会等到界面切到前台后自动刷新。
     */
    open fun notifyDataChange() {
        mRecycleViewHelper.notifyDataChange()
    }

    fun setEmptyStyle(icon: Int, msg: String) {
        mRecycleViewHelper.setEmpty(icon, msg)
    }

    fun setData(data: List<BaseItemViewModel<*>>, pageNum: Int = 1) {
        mRecycleViewHelper.setData(data, pageNum)
        finishLoad()
    }

    fun addData(data: List<BaseItemViewModel<*>>) {
        mRecycleViewHelper.addData(data)
    }

    fun addItem(index: Int, itemViewModel: BaseItemViewModel<*>) {
        mRecycleViewHelper.addItem(index, itemViewModel)
    }

    fun addItem(itemViewModel: BaseItemViewModel<*>) {
        mRecycleViewHelper.addItem(itemViewModel)
    }

    fun removeAt(index: Int) {
        mRecycleViewHelper.removeAt(index)
    }

    fun removeItem(itemViewModel: BaseItemViewModel<*>) {
        mRecycleViewHelper.removeItem(itemViewModel)
    }

    fun removeList(list: Collection<BaseItemViewModel<*>>) {
        mRecycleViewHelper.removeList(list)
    }

    fun clearData() {
        mRecycleViewHelper.clearData()
    }

    /**
     * 设置是否可以刷新
     */
    fun setRefreshEnable(enable: Boolean) {
        mRecycleViewHelper.setRefreshEnable(enable)
    }

    /**
     * 设置是否自动加载更多
     */
    fun setLoadMoreEnable(enable: Boolean) {
        mRecycleViewHelper.setLoadMoreEnable(enable)
    }

    /**
     * 设置列表为空时的显示样式
     */
    fun setListEmpty(icon: Int, msg: String) {
        mRecycleViewHelper.setEmpty(icon, msg)
    }


}